 package client;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.regex.Pattern;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleItemInformationProvider;
 import tools.Pair;
 import tools.Triple;





 public class MapleCharacterUtil
 {
/*  21 */   private static final Logger log = LogManager.getLogger(MapleCharacterUtil.class);
/*  22 */   private static final Pattern namePattern = Pattern.compile("^(?!_)(?!.*?_$)[a-zA-Z0-9_一-龥]+$");
/*  23 */   private static final Pattern petPattern = Pattern.compile("^(?!_)(?!.*?_$)[a-zA-Z0-9_一-龥]+$");

   public static boolean canCreateChar(String name, boolean gm) {
/*  26 */     return (getIdByName(name) == -1 && isEligibleCharName(name, gm));
   }

   public static boolean canChangePetName(String name) {
/*  30 */     return ((name.getBytes()).length >= 4 && (name.getBytes()).length <= 12 && petPattern.matcher(name).matches());
   }

   public static boolean isEligibleCharName(String name, boolean gm) {
/*  34 */     if ((name.getBytes()).length > 12) {
/*  35 */       return false;
     }
/*  37 */     if (gm) {
/*  38 */       return true;
     }
/*  40 */     return ((name.getBytes()).length >= 4 && namePattern.matcher(name).matches());
   }

   public static String makeMapleReadable(String in) {
/*  44 */     String wui = in.replace('I', 'i');
/*  45 */     wui = wui.replace('l', 'L');
/*  46 */     wui = wui.replace("rn", "Rn");
/*  47 */     wui = wui.replace("vv", "Vv");
/*  48 */     wui = wui.replace("VV", "Vv");
/*  49 */     return wui;
   }




   public static int getIdByName(String name) {
/*  56 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  57 */       PreparedStatement ps = con.prepareStatement("SELECT id FROM characters WHERE name = ?");
/*  58 */       ps.setString(1, name);
/*  59 */       ResultSet rs = ps.executeQuery();

/*  61 */       if (!rs.next()) {
/*  62 */         rs.close();
/*  63 */         ps.close();
/*  64 */         return -1;
       }
/*  66 */       int id = rs.getInt("id");
/*  67 */       rs.close();
/*  68 */       ps.close();

/*  70 */       return id;
/*  71 */     } catch (SQLException e) {
/*  72 */       log.error("error 'getIdByName' " + e);

/*  74 */       return -1;
     }
   }



   public static Pair<String, Integer> getNameById(int chrId, int world) {
/*  81 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  82 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE id = ? AND world = ?");
/*  83 */       ps.setInt(1, chrId);
/*  84 */       ps.setInt(2, world);
/*  85 */       ResultSet rs = ps.executeQuery();
/*  86 */       if (!rs.next()) {
/*  87 */         rs.close();
/*  88 */         ps.close();
/*  89 */         return null;
       }
/*  91 */       Pair<String, Integer> id = new Pair(rs.getString("name"), Integer.valueOf(rs.getInt("accountid")));
/*  92 */       rs.close();
/*  93 */       ps.close();
/*  94 */       return id;
/*  95 */     } catch (Exception e) {
/*  96 */       log.error("error 'getInfoByName' " + e);

/*  98 */       return null;
     }
   }
   public static boolean PromptPoll(int accountid) {
/* 102 */     PreparedStatement ps = null;
/* 103 */     ResultSet rs = null;
/* 104 */     boolean prompt = false;
/* 105 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection())
/* 106 */     { ps = con.prepareStatement("SELECT * FROM game_poll_reply WHERE AccountId = ?");
/* 107 */       ps.setInt(1, accountid);
/* 108 */       rs = ps.executeQuery();
/* 109 */       prompt = !rs.next(); }
/* 110 */     catch (SQLException sQLException)

     { try {
/* 113 */         if (ps != null) {
/* 114 */           ps.close();
         }
/* 116 */         if (rs != null) {
/* 117 */           rs.close();
         }
/* 119 */       } catch (SQLException sQLException1) {} } finally { try { if (ps != null) ps.close();  if (rs != null) rs.close();  } catch (SQLException sQLException) {} }


/* 122 */     return prompt;
   }

   public static boolean SetPoll(int accountid, int selection) {
/* 126 */     if (!PromptPoll(accountid)) {
/* 127 */       return false;
     }
/* 129 */     PreparedStatement ps = null;
/* 130 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection())
/* 131 */     { ps = con.prepareStatement("INSERT INTO game_poll_reply (AccountId, SelectAns) VALUES (?, ?)");
/* 132 */       ps.setInt(1, accountid);
/* 133 */       ps.setInt(2, selection);

/* 135 */       ps.execute(); }
/* 136 */     catch (SQLException sQLException)

     { try {
/* 139 */         if (ps != null) {
/* 140 */           ps.close();
         }
/* 142 */       } catch (SQLException sQLException1) {} } finally { try { if (ps != null) ps.close();  } catch (SQLException sQLException) {} }


/* 145 */     return true;
   }








   public static int Change_SecondPassword(int accid, String password, String newpassword) {
/* 156 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 157 */       String SHA1hashedsecond; PreparedStatement ps = con.prepareStatement("SELECT * from accounts where id = ?");
/* 158 */       ps.setInt(1, accid);
/* 159 */       ResultSet rs = ps.executeQuery();
/* 160 */       if (!rs.next()) {
/* 161 */         rs.close();
/* 162 */         ps.close();
/* 163 */         return -1;
       }
/* 165 */       String secondPassword = rs.getString("2ndpassword");
/* 166 */       String salt2 = rs.getString("salt2");
/* 167 */       if (secondPassword != null && salt2 != null) {
/* 168 */         secondPassword = LoginCrypto.rand_r(secondPassword);
/* 169 */       } else if (secondPassword == null && salt2 == null) {
/* 170 */         rs.close();
/* 171 */         ps.close();
/* 172 */         return 0;
       }
/* 174 */       if (!check_ifPasswordEquals(secondPassword, password, salt2)) {
/* 175 */         rs.close();
/* 176 */         ps.close();
/* 177 */         return 1;
       }
/* 179 */       rs.close();
/* 180 */       ps.close();

       try {
/* 183 */         SHA1hashedsecond = LoginCryptoLegacy.encodeSHA1(newpassword);
/* 184 */       } catch (Exception e) {
/* 185 */         return -2;
       }
/* 187 */       ps = con.prepareStatement("UPDATE accounts set 2ndpassword = ?, salt2 = ? where id = ?");
/* 188 */       ps.setString(1, SHA1hashedsecond);
/* 189 */       ps.setString(2, (String)null);
/* 190 */       ps.setInt(3, accid);
/* 191 */       if (!ps.execute()) {
/* 192 */         ps.close();
/* 193 */         return 2;
       }
/* 195 */       ps.close();
/* 196 */       return -2;
/* 197 */     } catch (SQLException e) {
/* 198 */       log.error("修改二级密码发生错误" + e);
/* 199 */       return -2;
     }
   }


   private static boolean check_ifPasswordEquals(String passhash, String pwd, String salt) {
/* 205 */     if (LoginCryptoLegacy.isLegacyPassword(passhash) && LoginCryptoLegacy.checkPassword(pwd, passhash))
     {
/* 207 */       return true; } 
/* 208 */     if (salt == null && LoginCrypto.checkSha1Hash(passhash, pwd))
/* 209 */       return true; 
/* 210 */     if (LoginCrypto.checkSaltedSha512Hash(passhash, pwd, salt)) {
/* 211 */       return true;
     }
/* 213 */     return false;
   }


   public static Triple<Integer, Integer, Integer> getInfoByName(String name, int world) {
/* 218 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 219 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE name = ? AND world = ?");
/* 220 */       ps.setString(1, name);
/* 221 */       ps.setInt(2, world);
/* 222 */       ResultSet rs = ps.executeQuery();
/* 223 */       if (!rs.next()) {
/* 224 */         rs.close();
/* 225 */         ps.close();
/* 226 */         return null;
       }
/* 228 */       Triple<Integer, Integer, Integer> id = new Triple(Integer.valueOf(rs.getInt("id")), Integer.valueOf(rs.getInt("accountid")), Integer.valueOf(rs.getInt("gender")));
/* 229 */       rs.close();
/* 230 */       ps.close();
/* 231 */       return id;
/* 232 */     } catch (Exception e) {
/* 233 */       log.error("error 'getInfoByName' " + e);

/* 235 */       return null;
     }
   }
   public static void setNXCodeUsed(String name, String code) throws SQLException {
/* 239 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 240 */       PreparedStatement ps = con.prepareStatement("UPDATE nxcode SET `user` = ?, `valid` = 0, time = CURRENT_TIMESTAMP() WHERE code = ?");
/* 241 */       ps.setString(1, name);
/* 242 */       ps.setString(2, code);
/* 243 */       ps.execute();
/* 244 */       ps.close();
     }
   }

   public static void sendNote(String to, String name, String msg, int fame) {
/* 249 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 250 */       PreparedStatement ps = con.prepareStatement("INSERT INTO notes (`to`, `from`, `message`, `timestamp`, `gift`) VALUES (?, ?, ?, ?, ?)");
/* 251 */       ps.setString(1, to);
/* 252 */       ps.setString(2, name);
/* 253 */       ps.setString(3, msg);
/* 254 */       ps.setLong(4, System.currentTimeMillis());
/* 255 */       ps.setInt(5, fame);
/* 256 */       ps.executeUpdate();
/* 257 */       ps.close();
/* 258 */     } catch (SQLException e) {
/* 259 */       log.error("Unable to send note" + e);
     }
   }

   public static Triple<Boolean, Integer, Integer> getNXCodeInfo(String code) throws SQLException {
/* 264 */     Triple<Boolean, Integer, Integer> ret = null;
/* 265 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 266 */       PreparedStatement ps = con.prepareStatement("SELECT `valid`, `type`, `item` FROM nxcode WHERE code = ?");
/* 267 */       ps.setString(1, code);
/* 268 */       ResultSet rs = ps.executeQuery();
/* 269 */       if (rs.next()) {
/* 270 */         ret = new Triple(Boolean.valueOf((rs.getInt("valid") > 0)), Integer.valueOf(rs.getInt("type")), Integer.valueOf(rs.getInt("item")));
       }
/* 272 */       rs.close();
/* 273 */       ps.close();
/* 274 */       return ret;
     }
   }

   public static void addToItemSearch(int itemId) {
/* 279 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 280 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 281 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM itemsearch WHERE itemid = ?");
/* 282 */       ps.setInt(1, itemId);
/* 283 */       ResultSet rs = ps.executeQuery();
/* 284 */       if (rs.next()) {
/* 285 */         int count = rs.getInt("count");
/* 286 */         PreparedStatement psu = con.prepareStatement("UPDATE itemsearch SET count = ? WHERE itemid = ?");
/* 287 */         psu.setInt(1, count + 1);
/* 288 */         psu.setInt(2, itemId);
/* 289 */         psu.executeUpdate();
/* 290 */         psu.close();
       } else {
/* 292 */         PreparedStatement psi = con.prepareStatement("INSERT INTO itemsearch (itemid, count, itemName) VALUES (?, ?, ?)");
/* 293 */         psi.setInt(1, itemId);
/* 294 */         psi.setInt(2, 1);
/* 295 */         psi.setString(3, ii.getName(itemId));
/* 296 */         psi.executeUpdate();
/* 297 */         psi.close();
       }
/* 299 */       rs.close();
/* 300 */       ps.close();
/* 301 */     } catch (SQLException e) {
/* 302 */       log.error(e);
     }
   }


   public static Pair<Integer, Integer> getCashByAccId(int AccId) {
/* 308 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 309 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM accounts WHERE id = ? ");
/* 310 */       ps.setInt(1, AccId);
/* 311 */       ResultSet rs = ps.executeQuery();
/* 312 */       if (!rs.next()) {
/* 313 */         rs.close();
/* 314 */         ps.close();
/* 315 */         return null;
       }
/* 317 */       Pair<Integer, Integer> id = new Pair(Integer.valueOf(rs.getInt("ACash")), Integer.valueOf(rs.getInt("mPoints")));
/* 318 */       rs.close();
/* 319 */       ps.close();
/* 320 */       return id;
/* 321 */     } catch (Exception e) {
/* 322 */       log.error("error 'getInfoByName' " + e);

/* 324 */       return null;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\client\MapleCharacterUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */